$(function(){
	//绑定确认框
	$().bootboxWindow_confirm();
	//时间选择初始化
	$('.getTime').datetimepicker({
		language: 'zh-CN',
		pickTime: false,
		todayBtn: true,
		autoclose: true,
		minView: '2',
		forceParse: false,
		format: "yyyy-mm-dd"
	});
	//自动完成初始化服务器数据
	//$('.autoCompleter').autocompleter({ source: 'path/to/get_data_request' });
	//自动完成初始化本地数据
	var data = [
	    { "value": "one", "label": "one" },
	    { "value": "two", "label": "two" },
	    { "value": "three", "label": "three" }
	];
	$(function () {
	    $('.autoCompleter').autocompleter({ source: data });
	});
 });
 //地图坐标选择
 var selectMap = function(){
	$('.selectMap').on('click', function(){
		var that = $(this);
		var coordinate = null;
		if(typeof $('.selectMap_box').attr('class') != 'undefined'){
			$('.selectMap_box').remove();
		}
		that.after(
			'<div class="selectMap_box">'+
				'<div class="search"><a id="selectMap_search_btn"><i class="fa fa-search"></i></a><input type="search" name="keyword" id="selectMap_input"></div>'+
				'<div id="selectMap"></div>'+
				'<div class="selectMap_btns">'+
					'<button type="button" class="btn btn-warning btn-sm" id="selectMap_btns_close"><i class="fa fa-times"></i>取消</button>'+
					'<button type="button" class="btn btn-info btn-sm" id="selectMap_btns_ok"><i class="fa fa-check"></i>确定</button>'+
				'</div>'+
			'</div>'
		);
		var  selectMapBox = $('.selectMap_box');
		//关闭
		$('#selectMap_btns_close').on('click',function(){
			selectMapBox.remove();
		});
		//确定
		$('#selectMap_btns_ok').on('click',function(){
			selectMapBox.remove();
			that.val(coordinate);
		});
		//地图初始化
		var map = new AMap.Map('selectMap',{
			resizeEnable: true,
			zoom: 10,
			keyboardEnable: false
	    });
	    //获取经纬度并标注
	    var clickEventListener = map.on('click', function(e) {
	    	map.clearMap();//清除全部标注
	        var lngAnddLat = [e.lnglat.getLng(),e.lnglat.getLat()];
		    var marker = new AMap.Marker({
		        position: lngAnddLat,
		        map: map
		    });
		    coordinate = lngAnddLat;
	    });
	    //收索地图地区
		var searchFunc = function(_that) {
			var val = _that.val();
			AMap.service(["AMap.PlaceSearch"], function() {
				var placeSearch = new AMap.PlaceSearch({ //构造地点查询类
					pageSize: 5,
					pageIndex: 1,
					city: "",
					map: map
				});
				//关键字查询
				placeSearch.search(val, function(status, result) {
					map.clearMap();
				});
			});
		}
		$('#selectMap_input').on('focus',function(){
	    	var that = $(this);
	    	that.on('keyup',function(event){
	    		var e = event || window.event || arguments.callee.caller.arguments[0];
		    	if(e && e.keyCode == 13){
		    		searchFunc(that);
		    	}
	    	});
	    });
	    $('#selectMap_search_btn').on('click',function(){
	    	searchFunc($('#selectMap_input'));
	    });
	});
}
//图片上传预览    IE是用了滤镜。
function previewImage(file,_showId) {
	//适应宽高
	var clacImgZoomParam = function(maxWidth, maxHeight, width, height) {
		var param = {'top': 0,'left': 0,'width': width,'height': height};
		if (width > maxWidth || height > maxHeight) {
			var rateWidth = width / maxWidth;
			var rateHeight = height / maxHeight;
	
			if (rateWidth > rateHeight) {
				param.width = maxWidth;
				param.height = Math.round(height / rateWidth);
			} else {
				param.width = Math.round(width / rateHeight);
				param.height = maxHeight;
			}
		}
		param.left = Math.round((maxWidth - param.width) / 2);
		param.top = Math.round((maxHeight - param.height) / 2);
		return param;
	}
	var maxwidth = 100;
	var maxheight = 50;
	var div = document.getElementById(_showId);
	var img;
	if (file.files && file.files[0]) {
		div.innerHTML = '<img id="imghead'+_showId+'">';
		img = document.getElementById('imghead'+_showId);
		img.onload = function() {
			var rect = clacImgZoomParam(maxwidth, maxheight, img.offsetWidth, img.offsetHeight);
			img.width = rect.width;
			img.height = rect.height;
			//img.style.marginLeft = rect.left+'px';
			img.style.marginTop = rect.top + 'px';
		}
		var reader = new FileReader();
		reader.onload = function(evt) {
			img.src = evt.target.result;
		}
		reader.readAsDataURL(file.files[0]);
	} else {
		//兼容IE
		var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
		file.select();
		var src = document.selection.createRange().text;
		div.innerHTML = '<img id="imghead'+_showId+'">';
		img = document.getElementById('imghead'+_showId);
		
		img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        var rect = clacImgZoomParam(maxwidth, maxheight, img.offsetWidth, img.offsetHeight);
        status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
        div.innerHTML = "<div id='divhead"+_showId+"' style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
	}
}